package com.ktg.mes.erpInventory.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.mes.md.domain.ItemType;
import com.ktg.mes.erpInventory.entity.T6ErpInventory;
import com.ktg.mes.erpInventory.mapper.T6ErpInventoryMapper;
import com.ktg.mes.erpInventory.service.IT6ErpInventoryService;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.domain.MdUnitMeasure;
import com.ktg.mes.md.service.IItemTypeService;
import com.ktg.mes.md.service.IMdUnitMeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 存货档案
 * @Author: jeecg-boot
 * @Date:   2022-08-14
 * @Version: V1.0
 */
@Service
public class T6ErpInventoryServiceImpl extends ServiceImpl<T6ErpInventoryMapper, T6ErpInventory> implements IT6ErpInventoryService {

    @Autowired
    private IItemTypeService iItemTypeService;

    @Autowired
    private IMdUnitMeasureService iMdUnitMeasureService;

    @Override
    public List<MdItem> queryListByLastPubufts(String pubufts) {
        List<T6ErpInventory> srcList = this.baseMapper.queryListByLastPubufts(Long.parseLong(pubufts));
        List<MdItem> distList = srcList.stream().map(item -> {
            MdItem dist = toMdItem(item);
//            BeanUtils.copyProperties(item,dist);
//            dist.setId(dist.getInvCode());
//            dist.setInvCatId(dist.getInvCatCode());
            return dist;
        }).collect(Collectors.toList());
        return distList;
    }

    public MdItem toMdItem(T6ErpInventory srcItem) {
        MdItem dist = new MdItem();
        dist.setItemCode(srcItem.getInvCode());
        dist.setItemName(srcItem.getInvName());
        dist.setSpecification(srcItem.getInvStd());
        MdUnitMeasure unitMeasure = iMdUnitMeasureService.selectMdUnitMeasureByMeasureCode(srcItem.getComUnitCode());
        if (unitMeasure != null) {
            dist.setUnitOfMeasure(unitMeasure.getMeasureName());
            dist.setMeasureNo(unitMeasure.getMeasureCode());
        }
        if (srcItem.getInvCatCode().startsWith("03")) {
            dist.setItemOrProduct("ITEM");
        }else{
            dist.setItemOrProduct("PRODUCT");
        }
        dist.setSafeStockFlag("N");
        dist.setEnableFlag("Y");
        ItemType itemType = iItemTypeService.selectItemTypeByInvCode(srcItem.getInvCatCode());
        if (itemType != null) {
            dist.setItemTypeId(itemType.getItemTypeId());
            dist.setItemTypeCode(itemType.getItemTypeCode());
            dist.setItemTypeName(itemType.getItemTypeName());
        }
        dist.setCurrencyName(srcItem.getCurrencyName());
        dist.setInvAddCode(srcItem.getInvAddCode());
        dist.setViscosity(srcItem.getViscosity());
        dist.setTag(srcItem.getTag());
        dist.setPackingType(srcItem.getPackingType());
        dist.setTaxRate(srcItem.getTaxRate());
        dist.setInvBoxStd(srcItem.getInvBoxStd());
        dist.setInvImages(srcItem.getInvImages());
        dist.setPubufts(srcItem.getExPubufts());
        return dist;
    }
}
